#!/bin/bash
host=${1:-"0.0.0.0"}
port=${2:-"7860"}
verbose=${3:-"1"}
cuda=${4:-"0"}
gpu_memory_utilization=${5:-0.1}
module=${6:-"tts"} # "tts|asr"
# module=${6:-"asr"} # "tts|asr"
# module=${6:-"tts|asr"} # "tts|asr"
tts_model_dir=${7:-"/opt/models/tts"}
# 语音合成模型地址
asr_model_dir=${8:-"/opt/models/asr/SenseVoiceSmall"}
# 语音识别模型地址
vad_model_dir=${9:-"/opt/models/asr/speech_fsmn_vad_zh-cn-16k-common-pytorch"}
# 语音活动检测模型地址，是用于识别音频中有效语音段的关键技术，常与语音识别、标点恢复等模块协同工作。
punc_model_dir=${10:-"/opt/models/asr/punc_ct-transformer_zh-cn-common-vocab272727-pytorch"}
# 标点恢复模型地址
spk_model_dir=${11:-"/opt/models/asr/speech_eres2netv2_sv_zh-cn_16k-common"}
spkd_model_dir=${12:-"/opt/models/asr/speech_campplus_speaker-diarization_common"}
# 说话人分离模型地址
denoise_model_dir=${13:-"/opt/models/asr/speech_frcrn_ans_cirm_16k"}
denoise_model_dir=${13:-"/opt/models/asr/speech_zipenhancer_ans_multiloss_16k_base"}
# 降噪模型地址
vad_model_dir=""
punc_model_dir=""
spk_model_dir=""
spkd_model_dir=""

export VLLM_USE_V1=0
export USE_WEBUI=1
export CUDA_LAUNCH_BLOCKING=1
export TORCH_USE_CUDA_DSA=1
export TTS_MODEL_DIR=$tts_model_dir
export ASR_MODEL_DIR=$asr_model_dir
export VAD_MODEL_DIR=$vad_model_dir
export PUNC_MODEL_DIR=$punc_model_dir
export SPK_MODEL_DIR=$spk_model_dir
export SPKD_MODEL_DIR=$spkd_model_dir
export DENOISE_MODEL_DIR=$denoise_model_dir
export MAX_NUM_SEQS=256
export GPU_MEMORY_UTILIZATION=$gpu_memory_utilization
export CUDA_VISIBLE_DEVICES=$cuda
export SERVICE_MODULE=$module
export SERVICE_HOST=$host
export SERVICE_PORT=$port
export SERVICE_VERBOSE=$verbose
export SERVICE_DTYPE="float16"
export LOG_LEVEL="error"
export TTS_DEFAULT='{"top_k":30,"top_p":0.8,"temperature":1,"repetition_penalty":10.0,"max_tokens":600,"sentences_bucket_max_size":4,"max_text_tokens_per_sentence":120}'
export VLLM_USE_V1 USE_WEBUI TTS_MODEL_DIR ASR_MODEL_DIR VAD_MODEL_DIR PUNC_MODEL_DIR SPK_MODEL_DIR SPKD_MODEL_DIR DENOISE_MODEL_DIR SERVICE_HOST SERVICE_PORT SERVICE_VERBOSE SERVICE_DTYPE MAX_NUM_SEQS GPU_MEMORY_UTILIZATION CUDA_VISIBLE_DEVICES SERVICE_MODULE LOG_LEVEL TTS_DEFAULT

pid=$(lsof -t -i :$port 2>/dev/null)
# 检查pid是否非空（即端口是否被占用）
if [ -n "$pid" ]; then
    kill -9 $pid
else
    echo "端口 $port 上没有找到进程。"
fi
clear
python=/opt/miniconda3/envs/indextts/bin/python
# $python plus/app_vllm.py
$python app.py